package com.example.demo.modules.sys.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.demo.modules.sys.vo.SysUserVo;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
			Exception arg3) throws Exception {
	
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
			ModelAndView arg3) throws Exception {
		
		
	}

	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler)
			throws Exception {
		
		//从session里面获取用户信息
		SysUserVo user = (SysUserVo)req.getSession().getAttribute("user");
		//如果从session得不到用户信息，就跳转到登录页面
		if(user==null){
			//根据用户名密码取不到用户信息，就提示用户名或密码错误
			resp.sendRedirect(req.getContextPath()+"/toLogin");
			return false;
		}
		return true;
	}

}
